package com.dynamic.mybatis.core.utils;

import com.baomidou.mybatisplus.core.toolkit.support.SFunction;

import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;

public class LambdaUtils {

    public static <T> String getFieldName(SFunction<T, ?> sFunction){
        Set<String> names = getMultipleFieldName(new SFunction[]{sFunction});
        return CollectionUtils.isNotEmpty(names)? names.stream().findFirst().orElse(null) : null;
    }
    public static <T> Set<String> getMultipleFieldName(SFunction<T, ?>... sFunction) {
        if(sFunction == null || sFunction.length < 1){
            return Collections.emptySet();
        }
       return Arrays.stream(sFunction).map(s ->{
            String method = com.baomidou.mybatisplus.core.toolkit.LambdaUtils.extract(s).getImplMethodName();
            String field = method.replaceFirst("^(get|is|set)", "");
            return Character.toLowerCase(field.charAt(0)) + field.substring(1);
        }).collect(Collectors.toSet());
    }
}
